package com.eqdd.yiqidian.ui.gongneng.renshi.train;

import android.Manifest;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.support.design.widget.BottomSheetDialog;
import android.view.View;

import com.eqdd.yiqidian.QRSignActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.ErweimaBean;
import com.eqdd.yiqidian.bean.rebuild.SignListBean;
import com.eqdd.yiqidian.service.LocationObservable;
import com.eqdd.yiqidian.share.ImgShareBuilder;
import com.eqdd.yiqidian.share.JShareDialog;
import com.eqdd.yiqidian.ui.ScanZXingActivity;
import com.eqdd.yiqidian.utils.BottomSheetUtil;
import com.eqdd.yiqidian.utils.FileUtil;
import com.eqdd.yiqidian.utils.GsonUtils;
import com.eqdd.yiqidian.utils.MyCodeUtil;
import com.eqdd.yiqidian.utils.ScanQRUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.File;


/**
 * @author吕志豪 .
 * @date 18-2-1  下午4:30.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class QRSignActivity extends BaseActivity {

    private BottomSheetDialog bottomSheet;
    private QRSignActivityCustom dataBinding;
    private SignListBean signListBean;
    private Bitmap mBitmap;
    private String address;
    private ScanQRUtil scanQRUtil;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_qr_sign);
        initTopTitleBar(View.VISIBLE, "");
        initTopRightImage(R.mipmap.ic_option_white, v -> {
            if (bottomSheet == null) {
                bottomSheet = BottomSheetUtil.getBottomSheet(this, "", (recyclerView, position, v1) -> {
                    bottomSheet.hide();
                    switch (position) {
                        case 0:
                            dataBinding.cardView.setDrawingCacheEnabled(true);
                            dataBinding.cardView.buildDrawingCache();
                            saveImageByUniversal(dataBinding.cardView.getDrawingCache());
                            break;
                        case 1:
                            showLoading();
                            MyCodeUtil.analyzeBitmap(mBitmap, new MyCodeUtil.OnAnalyzeCallback() {
                                @Override
                                public void onAnalyzeSuccess(String content) {
                                    hideLoading();
                                    scanQRUtil.dealQR(content, address);
                                }

                                @Override
                                public void onAnalyzeFailed() {
                                    hideLoading();
                                    ToastUtil.showShort("二维码解析失败");
                                }
                            });
                            break;
                        case 2:
                            dataBinding.cardView.setDrawingCacheEnabled(true);
                            dataBinding.cardView.buildDrawingCache();
                            File file = FileUtil.saveAsFile(dataBinding.cardView.getDrawingCache());
                            ImgShareBuilder builder = new ImgShareBuilder(this);
                            builder.setImagePath(file.getAbsolutePath())
                                    .setSource("签到")
                                    .setSourceOwner(AppConstent.user.getUsername())
                                    .build();
                            new JShareDialog(builder, this);
                            break;
                        case 3:
                            startActivity(ScanZXingActivity.class);
                            break;

                    }
                }, "保存图片", "识别图中二维码", "发送到", "扫描二维码");
            }
            bottomSheet.show();
        });
    }

    @Override
    public void initData() {
        scanQRUtil = new ScanQRUtil(this);
        RxPermissions.getInstance(this).request(Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe(isGrant -> LocationObservable.get().subscribe(aMapLocation -> {
                    if (aMapLocation.getErrorCode() == 0) {
                        address = aMapLocation.getAddress();
                    }
                }));
        signListBean = (SignListBean) getIntent().getSerializableExtra(Config.BEAN);
        ErweimaBean<SignListBean> signQRErweimaBean = new ErweimaBean<>();
        signQRErweimaBean.setType("3");
        signQRErweimaBean.setName("");
        signQRErweimaBean.setUgid("");
        signQRErweimaBean.setData(signListBean);
        MyCodeUtil.createImage(GsonUtils.createGsonString(signQRErweimaBean), 400, new MyCodeUtil.OnEncodeQRCodeCallback() {
            @Override
            public void onAnalyzeSuccess(Bitmap bitmap) {
                mBitmap = bitmap;
                dataBinding.ivQr.setImageBitmap(bitmap);
            }

            @Override
            public void onAnalyzeFailed() {

                System.out.println();
            }
        });
    }

    @Override
    public void setView() {
        dataBinding.tvTheme.setText(signListBean.getTheTheme());

    }

}
